import {APP_NAME, STATUS_HEIGHT} from "../../constant/index";
import {toast} from '../../utils/tool'
import eventBus from "../../utils/eventBus";

const app = getApp();
Page({
    data: {
        wifi: {
            name: '',
            password: '',
        },
        showMobile: false,
        showWifi: false,
        statusHeight: STATUS_HEIGHT,
        couponLen: 0,
        groupLen: 0,
        menuList: [
            {
                title: "团购验券",
                path: "/pages/group-buying/group-buying?type=2",
                image: 'yanquan',
            },
            {
                title: "常见问题",
                path: "/pages/wenti/wenti",
                image: 'wenti',
            }, {
                title: "WIFI连接",
                path: "",
                image: 'wifi',
                type: "fn",
                name: 'wifi'
            }, {
                title: "全部门店",
                path: "/pages/changestore/changestore",
                image: 'all-shop',
            },
            {
                title: "我要加盟",
                path: "/pages/join/join",
                image: 'jiameng',
            },
            {
                title: "联系商家",
                path: "",
                image: 'lianxi',
                type: 'fn',
                name: 'mobile'
            },
            {
                title: "客服中心",
                path: "",
                type: 'fn',
                name: 'callPhone',
                image: 'kefu',
            },
            // {
            //     title: "保洁中心",
            //     path: "",
            // },
            {
                title: "邀请有礼",
                path: "/pages/invitation/invitation",
                image: 'vip',
            },
            {
                title: "会员中心",
                path: "",
                image: 'vip',
            },
            // {
            //     title: "点餐订单",
            //     path: "/pages/order-list/order-list",
            //     image: 'vip',
            // },
        ],
        user: {},
        appName: APP_NAME
    },
    closeWifi() {
        this.setData({
            showWifi: false
        })
    },

    getWifiDesc() {
        let targetStore = wx.getStorageSync('firststore')
        if (!targetStore) return toast({title: '未选择店铺'})
        const {id: store_id} = targetStore
        app.ajax({
            url: '/api/room/storewifi',
            data: {store_id},
            success: res => {
                const {code, data, msg} = res
                if (code !== 1) return toast({title: msg})
                if (!data.length) return toast({title: '当前门店无wifi'})
                const [target] = data
                const {wifi_account, wifi_password} = target
                this.setData({
                    wifi: {
                        name: wifi_account,
                        password: wifi_password,
                    },
                    showWifi: true
                })
            }
        })
    },

    toUrl(ev) {
        const {path} = ev.currentTarget.dataset
        wx.navigateTo({
            url: path
        })
    },
    closePop() {
        this.setData({
            showMobile: false
        })
    },
    dyHand() {
        wx.navigateTo({
            url: '/pages/group-buying/group-buying?type=2'
        })
    },
    onShow() {
        this.userinfo()
        this.initUserCouponLen()
    },
    initUserCouponLen() {
        app.ajax({
            url: '/api/user/coupons',
            data: {page: 1},
            success: resp => {
                const {data} = resp
                let couponLen = 0
                if (data.hasOwnProperty('total')) couponLen = data.total
                this.setData({couponLen})
            }
        })
    },
    toWallet() {
        wx.navigateTo({
            url: '/pages/wallet/wallet'
        })
    },
    userinfo: function () {
        app.ajax({
            url: '/api/user/userinfo',
            success: res => {
                if (res.code === 1) {
                    const {data} = res
                    wx.setStorageSync('user', data);
                    this.setData({
                        user: data
                    })
                    app.setUserData(data)
                }
            }
        })
    },

    clickMenuItem(ev) {
        const {path, type, name} = ev.currentTarget.dataset
        if (type && type === 'fn') {
            if (name === 'callPhone') {
                return this.callPhone()
            }
            if (name === 'mobile') {
                this.setData({
                    showMobile: true
                })
                return;
            }
            if (name === 'wifi') {
                return this.getWifiDesc()
            }
            if (name === 'register') {
                return wx.navigateToMiniProgram({
                    appId: 'wxc4c033f5c53f00c5',
                    path: '/pages/login/login', //路径和携带的参数
                    envVersion: 'release',
                    success(res) {
                        // 打开成功
                    },
                    fail(res) {
                        // 打开失败
                    },
                    complete(res) {
                        // 调用结束  不管成功还是失败都执行
                    }
                    /**
                     * appId：跳转到的小程序app-id
                     * path：打开的页面路径，如果为空则打开首页，path 中 ? 后面的部分会成为 query，在小程序的 App.onLaunch、App.onShow 和 Page.onLoad的回调函数中获取query数据
                     * extraData：需要传递给目标小程序的数据，目标小程序可在 App.onLaunch、App.onShow 中获取到这份数据
                     * envVersion：要打开的小程序版本，有效值: develop（开发版），trial（体验版），release（正式版），仅在当前小程序为开发版或体验版时此参数有效，如果当前小程序是正式版，则打开的小程序必定是正式版
                     */
                })
            }
        }
        if (!path) {
            toast({title: '暂未开发'})
            return
        }
        wx.navigateTo({
            url: path
        })
    },

    callPhone: function () {
        wx.showModal({
            title: '确认拨打客服电话吗?',
            success: ({cancel}) => {
                if (cancel) return
                const tel = wx.getStorageSync('tel');
                if (!tel) return;
                wx.makePhoneCall({
                    phoneNumber: tel,
                })
            }
        })

    },

    my_wallet: function () {
        wx.navigateTo({
            url: '/pages/my_wallet/my_wallet',
        })
    },

    meituan: function () {
        wx.navigateTo({
            url: '/pages/meituan/meituan',
        })
    },
    coupon: function () {
        wx.navigateTo({
            url: '/pages/coupon/coupon',
        })
    },
    // 充值
    account_chongzhi: function () {
        wx.navigateTo({
            url: '/pages/account_chongzhi/account_chongzhi',
        })
    },

    updateUserInfo({type, value}) {
        if (type === 1) {
            return this.userinfo()
        }
        wx.setStorageSync('user', value);
        this.setData({
            user: value
        })
        app.setUserData(value)
    },

    onUnload() {
        eventBus.$off('updateUserInfo', this.updateUserInfo)
    },
    onLoad: async function (options) {
        eventBus.$on('updateUserInfo', this.updateUserInfo)
    }
});
